package com.liyuan.help_e.service.impl;

import com.liyuan.help_e.dao.QuestionDao;
import com.liyuan.help_e.dao.UserDao;
import com.liyuan.help_e.model.Question;
import com.liyuan.help_e.model.User;
import com.liyuan.help_e.service.QuestionService;
import com.liyuan.help_e.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    QuestionDao questionDao;
    @Autowired
    UserDao userDao;
    @Autowired
    SimpleDateFormat simpleDateFormat;
    @Autowired
    UserService userService;


    private void completeUserInfo_(Question question) {
        User user = userDao.selectUserByUserId(question.getUser().getId());
        question.getUser().setUsername(user.getUsername());
        question.getUser().setAvatar_url(user.getAvatar_url());
        question.getUser().setPassword(user.getPassword());
        question.getUser().setRole(user.getRole().getId());
    }

    private void completeUserInfo(Question[] questions) {
        for (Question question : questions) {
            User user = userDao.selectUserByUserId(question.getUser().getId());
            question.getUser().setUsername(user.getUsername());
            question.getUser().setAvatar_url(user.getAvatar_url());
            question.getUser().setPassword(user.getPassword());
            question.getUser().setRole(user.getRole().getId());
//            Collection collection=new ArrayList();
//            collection.add("2");
//            question.getUser().getRole().setAuthorities(collection);
            // TODO 此处已经证明，getAuthorities()是最后才调用的，故可在类似此处的地方将authorites set进去
        }
    }

    @Override
    public Question[] selectQuestionsByPage(Integer page, Integer size) {
        Question[] questions = questionDao.selectQuestionsByPage((page - 1) * size, size);
        completeUserInfo(questions);
        return questions;
    }

    @Override
    public Question[] selectQuestionsByReadCount(Integer size) {
        Question[] questions = questionDao.selectQuestionsByReadCount(size);
        completeUserInfo(questions);
        return questions;
    }

    @Override
    public Integer sumAllQuestions() {
        return questionDao.sumAllQuestions();
    }

    @Override
    public Integer sumQuestionsByTag(String type) {
        if (type.equals("study")) {
            type = "%学习%";
        } else if (type.equals("life")) {
            type = "%生活%";
        } else if (type.equals("resource")) {
            type = "%资源%";
        }
        return questionDao.sumQuestionsByTag(type);
    }

    @Override
    public Question[] selectQuestionsByPageAndTag(Integer page, Integer size, String type) {
        if (type.equals("study")) {
            type = "%学习%";
        } else if (type.equals("life")) {
            type = "%生活%";
        } else if (type.equals("resource")) {
            type = "%资源%";
        }
        Question[] questions = questionDao.selectQuestionsByPageAndTag((page - 1) * size, size, type);
        completeUserInfo(questions);
        return questions;
    }

    @Override
    public Question[] selectQuestionsByReadCountAndTag(Integer size, String type) {
        if (type.equals("study")) {
            type = "%学习%";
        } else if (type.equals("life")) {
            type = "%生活%";
        } else if (type.equals("resource")) {
            type = "%资源%";
        }
        Question[] questions = questionDao.selectQuestionsByReadCountAndTag(size, type);
        completeUserInfo(questions);
        return questions;
    }

    @Override
    public Question selectQuestionById(Integer id) {
        Question question = questionDao.selectQuestionById(id);
        completeUserInfo_(question);
        return question;
    }

    @Override
    public void insertQuestion(Question question) {
        question.setCreatedAt(simpleDateFormat.format(new Date()));
        question.setModifiedAt(question.getCreatedAt());
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        User user = userService.selectUserByUsername(username);
        question.setUser(user.getId());
        question.setCommentCount(0);
        question.setReadCount(0);
        question.setPraiseCount(0);
        questionDao.insertQuestion(question);
    }

    @Override
    public void updateQuestion(Question question) {
        Question question_ = questionDao.selectQuestionById(question.getId());
        question_.setTitle(question.getTitle());
        question_.setContent(question.getContent());
        question_.setTags(question.getTags());
        question_.setModifiedAt(simpleDateFormat.format(new Date()));
        questionDao.updateQuestion(question_);
    }

    @Override
    public Question[] selectPersonalQuestionsByUserId(Integer id, Integer page, int size) {
        Question[] questions = questionDao.selectQuestionsByUserId((page - 1) * size, size, id);
        completeUserInfo(questions);
        return questions;
    }
    @Override
    public Integer sumByUserId(Integer userId) {
        return questionDao.sumQuestionsByUserId(userId);
    }
}
